package site.marqstree.latte.ui.recycler;

import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.listener.GridSpanSizeLookup;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import java.util.ArrayList;
import java.util.List;
import site.marqstree.latte.core.app.Latte;
import site.marqstree.latte.ui.R;
import site.marqstree.latte.ui.banner.BannerCreator;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ui.recycler
 * 文件名: MultipleRecyclerAdapter
 * 创建者: marqstree
 * 创建时间: 2020/1/23 11:01
 * 描述: TODO
 */
public class MultipleRecyclerAdapter extends
        BaseMultiItemQuickAdapter<MultipleItemEntity, BaseViewHolder>
        implements
        GridSpanSizeLookup,
        OnItemClickListener,
        LoadMoreModule {

    //确保初始化一次Banner，防止重复Item加载
    private boolean mIsInitBanner = false;

    public MultipleRecyclerAdapter() {
        init();
    }

    public MultipleRecyclerAdapter(List<MultipleItemEntity> data){
        super(data);
        init();
    }

    private void init(){
        //设置不同的item布局
        addItemType(ItemType.TEXT, R.layout.item_multiple_text);
        addItemType(ItemType.IMAGE, R.layout.item_multiple_image);
        addItemType(ItemType.TEXT_IMAGE, R.layout.item_multiple_image_text);
        addItemType(ItemType.BANNER, R.layout.item_multiple_banner);
        //设置宽度监听
        setGridSpanSizeLookup(this);
        //打开动画
        setAnimationEnable(true);
        //关闭多次执行动画
        setAnimationFirstOnly(false);
    }

    //设置图片加载策略
    private static final RequestOptions RECYCLER_OPTIONS =
            new RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate();

    @Override
    protected void convert(@NonNull BaseViewHolder holder, MultipleItemEntity entity) {
        final String text;
        final String imageUrl;
        final ArrayList<String> bannerImages;
        switch (holder.getItemViewType()) {
            case ItemType.TEXT:
                text = entity.getField(MultipleFields.TEXT);
                holder.setText(R.id.text_single, text);
                break;
            case ItemType.IMAGE:
                imageUrl = entity.getField(MultipleFields.IMAGE_URL);
                Glide.with(Latte.getApplicationContext())
                        .load(imageUrl)
                        .apply(RECYCLER_OPTIONS)
                        .into((ImageView) holder.getView(R.id.img_single));
                break;
            case ItemType.TEXT_IMAGE:
                text = entity.getField(MultipleFields.TEXT);
                holder.setText(R.id.tv_multiple, text);
                imageUrl = entity.getField(MultipleFields.IMAGE_URL);
                Glide.with(Latte.getApplicationContext())
                        .load(imageUrl)
                        .apply(RECYCLER_OPTIONS)
                        .into((ImageView) holder.getView(R.id.img_multiple));
                break;
            case ItemType.BANNER:
                if (!mIsInitBanner) {
                    bannerImages = entity.getField(MultipleFields.BANNERS);
                    final ConvenientBanner<String> convenientBanner = holder.getView(R.id.banner_recycler_item);
                    BannerCreator.setDefault(convenientBanner, bannerImages, this);
                    mIsInitBanner = true;
                }
                break;
            default:
                break;
        }
    }

    //自定义Item的宽度
    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int viewType, int position) {
        int s = getData().get(position).getField(MultipleFields.SPAN_SIZE);
        return s;
    }

    //轮播图点击事件
    @Override
    public void onItemClick(int position) {
        Toast.makeText(Latte.getApplicationContext(),position+"",Toast.LENGTH_SHORT)
                .show();
    }
}